package com.xiaohe66.web.domain.im.service;

import com.xiaohe66.common.value.Paging;
import com.xiaohe66.web.domain.account.value.AccountId;
import com.xiaohe66.web.domain.im.agg.Message;
import com.xiaohe66.web.domain.im.repository.MessageRepository;
import com.xiaohe66.web.domain.im.value.MessageUnread;
import com.xiaohe66.web.domain.sys.sec.service.SecurityService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xiaohe
 * @since 2023-08-09 16:00
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class MessageService {

    private final SecurityService securityService;
    private final MessageRepository messageRepository;

    public void save(Message message) {

        securityService.checkCurrentAccountPermission(message.getCreateId());

        messageRepository.save(message);
    }

    public List<MessageUnread> countByAccountIdUnread(AccountId accountId) {

        securityService.checkCurrentAccountPermission(accountId);

        return messageRepository.countByAccountIdUnread(accountId);
    }

    public List<Message> pageOfAccountId(Paging paging, AccountId createId, AccountId accountId) {

        securityService.checkCurrentAccountPermission(accountId);

        return messageRepository.pageOfAccountId(paging, createId, accountId);
    }
}
